home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: peer-news.britain.eu.net!warwick!bsmail!talisker!nathan
- From: nathan@pact.srf.ac.uk (Nathan Sidwell)
- Subject: Re: do || die;
- Message-ID: <DnwJKs.KyJ@uns.bris.ac.uk>
- Sender: usenet@uns.bris.ac.uk (Usenet news owner)
- Nntp-Posting-Host: talisker.pact.srf.ac.uk
- Organization: Inmos
- X-Newsreader: TIN [version 1.2 PL2]
- References: <1996Mar7.052636.59812@ucl.ac.uk>
- Date: Thu, 7 Mar 1996 14:43:40 GMT
-
- Timothy Slidel (slidel@bsm.bioc.ucl.ac.uk) wrote:
- : Whilst it seems to work ok - is it considered bad style to use
- : logical operator expressions as conditional statements on their own, a la
- : Perl?
-
- : e.g. if I want to decrement i only when it is != 0:
-
- : i && i--;
-
- You should write to make your intent clear.
-
- The expressions 'i && i--' looks like a simple boolean conditional.
- However, your intent is the sideffect of i--. As such
- if(i)
- i--;
- would be much clearer.
-
- Suppose I have a processing function which, if it fails I want to set an
- error flag. What is the important thing here? setting the error flag
- or processing the data? probably processing the data. Which of the following
- two code fragments more clearly conveys what is happening?
-
- if(processing_function())
- error = 1;
- or
- error |= processing_function();
-
- Another example. Suppose I want to set a variable to one of two expressions
- based on a flag? Is the thing to emphases the test or the assignment?
-
- if(flag)
- var = expr1;
- else
- var = expr2;
- or
- var = flag ? expr1 : expr2
-
- It depends on circumstances (and therein lies the art).
-
- nathan
-
- --
- Nathan Sidwell Holder of the Xmris home page
- Chameleon Architecture Group at SGS-Thomson, formerly Inmos
- http://www.pact.srf.ac.uk/~nathan/ Tel 0117 9707182
- nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
-